/*
 * ReaderPanel.java
 *
 * Created on July 11, 2008, 1:14 PM
 */
package androidpos.gui;

import androidpos.network.Sync;
import androidpos.util.TransactionHandler;

/**
 *
 * @author  user
 */
public class ReaderPanel extends javax.swing.JPanel {

    private Thread syncThread;
    private Sync syncRunnable;
    private TransactionHandler transactionHandler;
    /** Creates new form ReaderPanel */
    public ReaderPanel() {
        initComponents();
    }
    public void setTransactionHandler(TransactionHandler transactionHandler){
        this.transactionHandler = transactionHandler;
    }
    public TransactionHandler getTransactionHandler(){
        return transactionHandler;
    }
    public void setClientHost(String ip) {
        clientHostLabel.setText(ip);
    }
    public void setClientName(String name){
        clientNameLabel.setText(name);
    }
    public String getTotalPrice() {
        return totalPriceLabel.getText();
    }

    public String getUnit() {
        return unitLabel.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        productPanel = new androidpos.gui.ProductPanel();
        mobilePhoneLabel = new javax.swing.JLabel();
        clientHostLabel = new javax.swing.JLabel();
        totalPrice = new javax.swing.JLabel();
        totalPriceLabel = new javax.swing.JLabel();
        openConnectionButton = new javax.swing.JButton();
        startPayButton = new javax.swing.JButton();
        unitLabel = new javax.swing.JLabel();
        calculateButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        sendPaymentInfoButton = new javax.swing.JButton();
        cardNameLabel = new javax.swing.JLabel();
        clientNameLabel = new javax.swing.JLabel();

        mobilePhoneLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        mobilePhoneLabel.setText("Mobile phone: ");

        clientHostLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        clientHostLabel.setForeground(new java.awt.Color(255, 0, 0));

        totalPrice.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        totalPrice.setText("Total price: ");

        totalPriceLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        totalPriceLabel.setForeground(new java.awt.Color(0, 204, 0));
        totalPriceLabel.setText("0.0");

        openConnectionButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        openConnectionButton.setText("Open connection");
        openConnectionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openConnectionButtonActionPerformed(evt);
            }
        });

        startPayButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        startPayButton.setText("Start pay");
        startPayButton.setEnabled(false);
        startPayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startPayButtonActionPerformed(evt);
            }
        });

        unitLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        unitLabel.setText("THB");

        calculateButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        calculateButton.setText("Calculate");
        calculateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculateButtonActionPerformed(evt);
            }
        });

        cancelButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.setEnabled(false);
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        sendPaymentInfoButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        sendPaymentInfoButton.setText("Send Payment");
        sendPaymentInfoButton.setEnabled(false);
        sendPaymentInfoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendPaymentInfoButtonActionPerformed(evt);
            }
        });

        cardNameLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        cardNameLabel.setText("Card name: ");

        clientNameLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        clientNameLabel.setForeground(new java.awt.Color(51, 51, 255));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(productPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cardNameLabel)
                    .addComponent(mobilePhoneLabel)
                    .addComponent(totalPrice))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(cancelButton)
                        .addComponent(startPayButton)
                        .addComponent(sendPaymentInfoButton)
                        .addComponent(openConnectionButton)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(totalPriceLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(unitLabel)
                            .addGap(18, 18, 18)
                            .addComponent(calculateButton))
                        .addComponent(clientHostLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(clientNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(productPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(clientHostLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                    .addComponent(mobilePhoneLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cardNameLabel)
                    .addComponent(clientNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalPriceLabel)
                    .addComponent(totalPrice)
                    .addComponent(unitLabel)
                    .addComponent(calculateButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(openConnectionButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sendPaymentInfoButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(startPayButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cancelButton)
                .addGap(45, 45, 45))
        );
    }// </editor-fold>//GEN-END:initComponents

private void openConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openConnectionButtonActionPerformed
// TODO add your handling code here:
    openConnectionButton.setEnabled(false);
    cancelButton.setEnabled(true);
    syncRunnable = new Sync(this);
    syncThread = new Thread(syncRunnable);
    syncThread.start();
}//GEN-LAST:event_openConnectionButtonActionPerformed
    public void setSendInfoButtonEnabled(boolean truth) {
        sendPaymentInfoButton.setEnabled(truth);
    }

    public void setStartPayButtonEnabled(boolean truth) {
        startPayButton.setEnabled(truth);
    }

    public void freezeCancelButton() {
        cancelButton.setEnabled(false);
    }
    public void resetAll(){
        clientHostLabel.setText("");
        clientNameLabel.setText("");
        openConnectionButton.setEnabled(true);
        sendPaymentInfoButton.setEnabled(false);
        startPayButton.setEnabled(false);
        cancelButton.setEnabled(false);
    }
private void startPayButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startPayButtonActionPerformed
// TODO add your handling code here:
    syncRunnable.sendToTheBank();
}//GEN-LAST:event_startPayButtonActionPerformed

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
// TODO add your handling code here:
    syncRunnable.stop();
}//GEN-LAST:event_cancelButtonActionPerformed

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calculateButtonActionPerformed
// TODO add your handling code here:
    totalPriceLabel.setText(productPanel.getTotalPrice()+"");//GEN-LAST:event_calculateButtonActionPerformed
    }
private void sendPaymentInfoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendPaymentInfoButtonActionPerformed
// TODO add your handling code here:
    syncRunnable.sendPaymentInformation();
}//GEN-LAST:event_sendPaymentInfoButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton calculateButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel cardNameLabel;
    private javax.swing.JLabel clientHostLabel;
    private javax.swing.JLabel clientNameLabel;
    private javax.swing.JLabel mobilePhoneLabel;
    private javax.swing.JButton openConnectionButton;
    private androidpos.gui.ProductPanel productPanel;
    private javax.swing.JButton sendPaymentInfoButton;
    private javax.swing.JButton startPayButton;
    private javax.swing.JLabel totalPrice;
    private javax.swing.JLabel totalPriceLabel;
    private javax.swing.JLabel unitLabel;
    // End of variables declaration//GEN-END:variables
}
